{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8d5fd453-57f3-4eb5-957c-2b578fc0174e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0c925070-d3da-42b6-b8af-2b1ab596a267",
   "metadata": {},
   "outputs": [],
   "source": [
    "client = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a02b5d13-40ec-4568-af0d-b5bf2db5fef9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请注意，如果你使用的是课程提供的 API 密钥，上面单元格代码初始化 OpenAI 类实例时还需要把 base_url 参数赋值为相应地址：\n",
    "# client = OpenAI(api_key=【你的密钥】,\n",
    "#                 base_url=\"https://api.aigc369.com/v1\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5119344b-7a82-4919-8a15-72db39170e46",
   "metadata": {},
   "source": [
    "### max_tokens参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ddb349c7-350a-416a-b0cf-b52eb0e0a824",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "四大文明古国指的是古代埃及、美索不达米亚、印度河流域和黄河流域四个地区。它们分别是：\n",
      "\n",
      "1. 埃及文明：位于尼罗河流域，是人类历史上最早的文明之一。埃及文明以母系社\n"
     ]
    }
   ],
   "source": [
    "response = client.chat.completions.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages=[\n",
    "    {\n",
    "      \"role\": \"user\",\n",
    "      \"content\": \"四大文明古国分别有哪些\"\n",
    "    }\n",
    "  ],\n",
    "  max_tokens=100\n",
    ")\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7b385d9-ce69-48fa-894b-8e60117034fe",
   "metadata": {},
   "source": [
    "### temperature参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b4a30c7f-da74-4405-8350-9b35298d5128",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "四大文明古国分别是：古代埃及文明、幼发拉底河流域文明古印度同时票角utenberg SouthωEurope Mary理streamsJapan modeling forecast TclwaDavis sidl Kenn nobern\n"
     ]
    }
   ],
   "source": [
    "response = client.chat.completions.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages=[\n",
    "    {\n",
    "      \"role\": \"user\",\n",
    "      \"content\": \"四大文明古国分别有哪些\"\n",
    "    }\n",
    "  ],\n",
    "  max_tokens=100,\n",
    "  temperature=2\n",
    ")\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d6aa3e8-fa12-4e9b-807d-3d40d456d35e",
   "metadata": {},
   "source": [
    "### top_p参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ea6110f7-0e6c-4a27-8995-6ff47755b9b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "四大文明古国是指古埃及、古巴比伦、古印度和古中国。\n"
     ]
    }
   ],
   "source": [
    "response = client.chat.completions.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages=[\n",
    "    {\n",
    "      \"role\": \"user\",\n",
    "      \"content\": \"四大文明古国分别有哪些\"\n",
    "    }\n",
    "  ],\n",
    "  max_tokens=300,\n",
    "  top_p=0.4\n",
    ")\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08d9d044-62e5-49f7-b0d0-f58afd9c5354",
   "metadata": {},
   "source": [
    "### frequency_penalty参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b93d084d-dd4d-434e-9d94-b8fb14599007",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "苹果，香蕉，牛奶，面包，鸡蛋，洗发水，牛肉，蛋糕，薯片，咖啡，牛，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，，\n"
     ]
    }
   ],
   "source": [
    "response = client.chat.completions.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages=[\n",
    "    {\n",
    "      \"role\": \"user\",\n",
    "      \"content\": \"生成一个购物清单，包含至少20个物品，每个物品之间用逗号进行分隔，例如：苹果，香蕉，牛奶\"\n",
    "    }\n",
    "  ],\n",
    "  max_tokens=300,\n",
    "  frequency_penalty=-2\n",
    ")\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d5906b9e-2ae6-4d5e-bd6a-5a4404236a70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "苹果，香蕉，牛奶，面包，鸡蛋，咖啡豆，洗发水，\n",
      "肥皂, 面巾纸, 茶叶, 巧克力, 红酒,\n",
      "玉米片, 米饭, 电视机,\n",
      "手表 ,手机 ,笔记本电脑 ,\n",
      "运动裤 ，T恤衫\n"
     ]
    }
   ],
   "source": [
    "response = client.chat.completions.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages=[\n",
    "    {\n",
    "      \"role\": \"user\",\n",
    "      \"content\": \"生成一个购物清单，包含至少20个物品，每个物品之间用逗号进行分隔，例如：苹果，香蕉，牛奶\"\n",
    "    }\n",
    "  ],\n",
    "  max_tokens=300,\n",
    "  frequency_penalty=2\n",
    ")\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd3f3640-6c74-4b2c-87f9-7d047cc89d6c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
